<template>
  <div v-if="show" class="settings-dialog">
    <div class="dialog-content">
      <h3>设置</h3>
      
      <div class="settings-section">
        <h4>主题</h4>
        <div class="theme-selector">
          <div
            v-for="(theme, name) in themes"
            :key="name"
            class="theme-option"
            :class="{ active: currentTheme === name }"
            @click="handleThemeChange(name)"
          >
            <div class="theme-preview">
              <div class="preview-square" :style="{ background: theme.lightSquare }"></div>
              <div class="preview-square" :style="{ background: theme.darkSquare }"></div>
            </div>
            <div class="theme-name">{{ theme.name }}</div>
          </div>
        </div>
      </div>

      <div class="settings-section">
        <h4>音效</h4>
        <div class="sound-settings">
          <label>
            <input
              type="checkbox"
              v-model="soundSettings.enabled"
              @change="handleSoundToggle('enabled')"
            >
            启用音效
          </label>

          <div class="volume-control">
            <span>音量</span>
            <input
              type="range"
              min="0"
              max="1"
              step="0.1"
              v-model="soundSettings.volume"
              @change="handleVolumeChange"
            >
          </div>

          <label>
            <input
              type="checkbox"
              v-model="soundSettings.moveSound"
              @change="handleSoundToggle('moveSound')"
            >
            移动音效
          </label>

          <label>
            <input
              type="checkbox"
              v-model="soundSettings.captureSound"
              @change="handleSoundToggle('captureSound')"
            >
            吃子音效
          </label>

          <label>
            <input
              type="checkbox"
              v-model="soundSettings.checkSound"
              @change="handleSoundToggle('checkSound')"
            >
            将军音效
          </label>
        </div>
      </div>

      <div class="dialog-buttons">
        <button @click="$emit('close')">关闭</button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useChessTheme } from '../composables/useChessTheme'
import { useChessSoundSettings } from '../composables/useChessSoundSettings'

const props = defineProps<{
  show: boolean
}>()

const emit = defineEmits<{
  (e: 'close'): void
}>()

const { currentTheme, setTheme, getThemes } = useChessTheme()
const { settings: soundSettings, toggleSound, setVolume, loadSettings } = useChessSoundSettings()

const themes = getThemes()

const handleThemeChange = (themeName: string) => {
  setTheme(themeName)
}

const handleSoundToggle = (type: string) => {
  toggleSound(type as keyof typeof soundSettings.value)
}

const handleVolumeChange = (event: Event) => {
  const target = event.target as HTMLInputElement
  setVolume(parseFloat(target.value))
}

onMounted(() => {
  loadSettings()
})
</script>

<style scoped>
.settings-dialog {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.dialog-content {
  background: white;
  padding: 20px;
  border-radius: 8px;
  min-width: 400px;
}

.settings-section {
  margin: 20px 0;
}

.theme-selector {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.theme-option {
  cursor: pointer;
  padding: 10px;
  border: 2px solid transparent;
  border-radius: 4px;
}

.theme-option.active {
  border-color: #4CAF50;
}

.theme-preview {
  display: flex;
  height: 40px;
}

.preview-square {
  width: 50%;
  height: 100%;
}

.theme-name {
  text-align: center;
  margin-top: 5px;
}

.sound-settings {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.volume-control {
  display: flex;
  align-items: center;
  gap: 10px;
}

.volume-control input {
  flex: 1;
}

.dialog-buttons {
  margin-top: 20px;
  text-align: right;
}

button {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  background-color: #4CAF50;
  color: white;
  cursor: pointer;
}

button:hover {
  background-color: #45a049;
}
</style> 